# -*- coding: utf-8 -*-
import pygame
import player
from common import *

HANDLED = 1
CONTINUE_HANDLE = 2
ERROR_HAPPENED = 3

STATUS_SUCC = 0
STATUS_FAIL = 1

class Event:
    def __init__(self, event_id):
        self.event_id = event_id
        self.extra_data = {}
        self.state = CONTINUE_HANDLE
        self.error_msg = str('')
        
    def type(self):
        return self.event_id
        
    def get_state(self):
        return self.state
    
    def set_state(self, state):
        self.state = state
        
class QuitEvent(Event):
    event_id = 0
    def __init__(self):
        Event.__init__(self, self.event_id)
        
#class KeyDownEvent(Event):
#    event_id = 1
#    def __init__(self, key_maps):
#        Event.__init__(self, self.event_id)
#        self.extra_data[D_KEY_MAPS] = key_maps
#        
#class MousePressedEvent(Event):
#    event_id = 2
#    def __init__(self, mouse_bt_maps):
#        Event.__init__(self, self.event_id)
#        self.extra_data[D_MOUSE_BUTTONS] = mouse_bt_maps
        
class FrameEvent(Event):
    event_id = 1
    def __init__(self):
        Event.__init__(self, self.event_id)
        
class MoveEvent(Event):
    event_id = 2
    def __init__(self, player_id, pos):
        Event.__init__(self, self.event_id)
        self.id = player_id
        self.pos = pos
        
class RegEvent(Event):
    event_id = 3
    def __init__(self, name, role):
        Event.__init__(self, self.event_id)
        self.name = name
        self.role = role
        
class LoginEvent(Event):
    event_id = 4
    def __init__(self, name):
        Event.__init__(self, self.event_id)
        self.name = name
        
class ConfirmEvent(Event):
    event_id = 5
    def __init__(self, status = 0):
        Event.__init__(self, self.event_id)
        self.status = status
        
class PlayerOnLineEvent(Event):
    event_id = 6
    def __init__(self, id, hp, pos):
        Event.__init__(self, self.event_id)
        self.id = id
        self.hp = hp
        self.pos = pos
        
class PygameEventTranslator:
    '''
    将pygame的event转换为自定义event类型的帮助类
    '''
    def __init__(self):
        self.type_map = {
                         pygame.QUIT : self.__quit_creator,
#                         pygame.KEYDOWN : self.__keydown_creator,
                         pygame.MOUSEBUTTONDOWN: self.__mousepressed_creator
                         }
        
    def __quit_creator(self, event):
        return QuitEvent()
    
#    def __keydown_creator(self, event):
#        keydown_event = KeyDownEvent(pygame.key.get_pressed())
#        return keydown_event
    
    def __mousepressed_creator(self, event):
        my_player = player.get_player_sprite_center().get_my_player()
        pos = pygame.mouse.get_pos()
        move_event = MoveEvent(my_player.id, pos)
        return move_event
        
    def translate(self, pygame_event):
        '''
        将pygame_event转换为自定义的event
        pygame_event:要转换的pygame的event对象
        返回：如果能够转换则返回转换后的event对象,否则返回None
        '''
        if not pygame_event.type in self.type_map:
            return None
        return self.type_map[pygame_event.type](pygame_event)
        